﻿using UnityEngine;
using System.Collections;
using System.IO;

public class CsvFileReader : StreamReader
{
    // Methods
    public CsvFileReader(Stream StreamIn) : base(StreamIn)
    {
    }

    public CsvFileReader(string FileName) : base(FileName)
    {
    }

    public bool ReadRow(CsvRow Row)
    {
        Row.LineText = this.ReadLine();
        if (string.IsNullOrEmpty(Row.LineText))
        {
            return false;
        }
        int num = 0;
        int index = 0;
        while (num < Row.LineText.Length)
        {
            string str;
            if (Row.LineText[num] == '"')
            {
                num++;
                int startIndex = num;
                while (num < Row.LineText.Length)
                {
                    if (Row.LineText[num] == '"')
                    {
                        num++;
                        if ((num >= Row.LineText.Length) || (Row.LineText[num] != '"'))
                        {
                            num--;
                            break;
                        }
                    }
                    num++;
                }
                str = Row.LineText.Substring(startIndex, num - startIndex).Replace("\"\"", "\"");
            }
            else
            {
                int num4 = num;
                while ((num < Row.LineText.Length) && (Row.LineText[num] != ','))
                {
                    num++;
                }
                str = Row.LineText.Substring(num4, num - num4);
            }
            if (index < Row.Count)
            {
                Row[index] = str;
            }
            else
            {
                Row.Add(str);
            }
            index++;
            while ((num < Row.LineText.Length) && (Row.LineText[num] != ','))
            {
                num++;
            }
            if (num < Row.LineText.Length)
            {
                num++;
            }
        }
        while (Row.Count > index)
        {
            Row.RemoveAt(index);
        }
        return (Row.Count > 0);
    }
}


